package com.baibei.ebec.user.messagenotice;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.baibei.ebec.sdk.TokenInfo;
import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.ebec.user.systemmessage.SystemMessageContract;
import com.baibei.model.MessageInfo;
import com.baibei.model.MsgNoticeInfo;
import com.baibei.model.UserInfo;
import com.baibei.module.BasicFragment;
import com.baibei.module.LazyLoadFragment;
import com.baibei.module.LoadMoreAdapter;
import com.baibei.order.bill.SimpleRefreshListener;
import com.baibei.order.bill.sticky_header.StickyHeaderScrollListener;
import com.baibei.widget.CommonRefreshLayout;
import com.blankj.utilcode.utils.LogUtils;
import com.rae.swift.session.SessionManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;


public class SystemMessageNoticeFragment extends BasicFragment implements SystemMsgNoticeContract.SystemMsgNoticeView{

    @BindView(R2.id.recycler_view)
    RecyclerView mRecyclerView;
    @BindView(R2.id.refresh_layout)
    CommonRefreshLayout mRefreshLayout;
    LoadMoreAdapter mLoadMoreAdapter;
    MessageNoticeAdapter mAdapter;
    private String accessToken;
    private String userId;

    private SystemMsgNoticeContract.Presenter mPresenter;
    public SystemMessageNoticeFragment() {
    }
    public static SystemMessageNoticeFragment newInstance(String param1, String param2) {
        SystemMessageNoticeFragment fragment = new SystemMessageNoticeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = inject(SystemMsgNoticeContract.Presenter.class);
        mAdapter = new MessageNoticeAdapter(new ArrayList<MsgNoticeInfo>());
        TokenInfo userToken = SessionManager.getDefault().getUserToken();
        UserInfo userInfo = SessionManager.getDefault().getUser();
        accessToken = userToken.getAccessToken();
        userId = userInfo.getUserId()+"";
        mPresenter.getMsgNoticeList(userId,accessToken);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
        //添加滚动监听

        mRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout ptrFrameLayout) {
                mPresenter.getMsgNoticeList(userId,accessToken);
            }
        });
    }

    @Override
    public void onDestroy() {
        if (mPresenter!=null)
        {
            mPresenter.destroy();
        }
        super.onDestroy();
    }
    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.fragment_system_message_notice;
    }

    @Override
    public void hideRefreshLayout() {
        if (mRefreshLayout != null && mRefreshLayout.isRefreshing()) {
            mRefreshLayout.refreshComplete();
        }
    }

    @Override
    public void onLoadNoticeMsg(List<MsgNoticeInfo> msgNoticeInfos) {
        mAdapter.setInfos(msgNoticeInfos);
        mAdapter.notifyDataSetChanged();
    }
}
